package logic

import (
	"context"
	"time"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"gorm.io/gorm"

	"2106A-zg6/baidu/srv/internal/svc"
	"2106A-zg6/baidu/srv/models"
	"2106A-zg6/baidu/srv/pb/baidu"
	"2106A-zg6/baidu/srv/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileUploadLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadLogic {
	return &FileUploadLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FileUploadLogic) FileUpload(in *baidu.FileUploadRequest) (*baidu.FileUploadResponse, error) {

	err := models.FileUpload(&models.File{
		UserId: in.UserId,
		File:   in.File,
		Model: gorm.Model{
			CreatedAt: time.Now(),
			UpdatedAt: time.Now(),
		},
	})
	if err != nil {
		return nil, status.Error(codes.InvalidArgument, err.Error())
	}

	//打印日志 输出到日志文件
	utils.LogFile(in.UserId + "文件上传成功" + in.File)

	return &baidu.FileUploadResponse{}, nil
}
